package com.weiyun.shortlink.admin.toolkit;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/***
 *   @Author:weiyun
 *
 */
public final class RandomGenerator {
    // 定义可用字符集（数字和大小写字母）
    private static final String CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    public static String generateRandomString(){
        return  generateRandomString(6);
    }
    /**
     * 生成指定长度的随机字符串
     * @return 随机字符串
     */
    public static String generateRandomString(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("长度必须为正整数");
        }

        Random random = ThreadLocalRandom.current();
        StringBuilder sb = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(randomIndex));
        }

        return sb.toString();
    }

}
